package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Auther: zhaoss
 * @Date: 2023/4/29 - 04 - 29 - 16:23
 * @Description: com.sky.service.impl
 * @version: 1.0
 */
@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页查询菜品套餐
     * @param dto
     * @return
     */
    @Override
    public Result page(CategoryPageQueryDTO dto) {
        //非空判断
        if (dto.getPage()==null){
            dto.setPage(0);
        }
        if (dto.getPageSize() == null) {
            dto.setPageSize(5);
        }
        //分页插件
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //dao
        Page<Category> categoryPage = categoryMapper.page(dto);
        long total = categoryPage.getTotal();
        List<Category> result = categoryPage.getResult();
        PageResult pageResult = new PageResult(total, result);
        return Result.success(pageResult);
    }


    /**
     * 新增分类
     * @return
     */
    @Override
    @Transactional
    public Result add(CategoryDTO dto) {
        Category byName = categoryMapper.getByName(dto.getName());
        if (byName == null) {
            return Result.error(MessageConstant.INPUT_ERROR);
        }
        if (dto.getType()==null){
            dto.setType(1);
        }
        //添加字段
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        category.setStatus(1);
        /*category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());*/

        categoryMapper.add(category);

        return Result.success(category);
    }

    /**
     * type查询
     * @param type
     * @return
     */
    @Override
    @Cacheable(value = "categoryCache",key = "'category'")
    public Result getByType(Integer type) {
        List<Category> list = categoryMapper.getByType(type);

        return Result.success(list);
    }

    /**
     * 修改
     * @return
     */
    @Override
    @Transactional
    public Result put(CategoryDTO dto) {
        if (dto.getId()==null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        category.setStatus(1);
        /*category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/

        categoryMapper.put(category);
        return Result.success(category);
    }


    /**
     * 修改分类状态
     * @param status
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result updateEmpStatus(Integer status, Long id) {
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        /*category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        categoryMapper.put(category);

        return Result.success(category);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result delete(Long id) {
        //非空判断
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //判断分类下是否还有菜品，有就不能删
        Integer byCategoryId = dishMapper.getByCategoryId(id);
        if (byCategoryId>0) {
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //判断套餐分类下是否还有套餐，有就不能删
        Integer count = setmealMapper.countByCategoryId(id);
        if (count>0) {
            return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
        return Result.success();
    }
}
